﻿using NoteHelper.IBusiness;
using NoteHelper.Model;
using NoteHelper.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace NoteHelper.Sys
{
	public class Sysm003Controller
	{
		private int rowIndex;
		private IBaseView view;
        private IProjectFormManager projectFormManager;
        public Sysm003Controller(IBaseView baseView, IProjectFormManager manager)
		{
			view = baseView;

			view.Insert += Form_Insert;
			view.Edit += Form_Edit;
			view.Remove += Form_Remove;
			view.Copy += Form_Copy;
			view.Query += Form_Query;
			view.Accept += Form_Accept;
			view.Cancel += Form_Cancel;
			view.Print += Form_Print;
			view.Help += Form_Help;
            Form_Cancel(null, null);
            projectFormManager = manager;
            var ht = view.RowData;
            ht["ObjectID"] = "-1";
            Query(ht);
        }
		private void Form_Insert(object sender, EventArgs e)
		{
			view.RowData = null;
			view.Cmd = FormCommand.Insert;
			view.BeginEdit();
		}
		private void Form_Edit(object sender, EventArgs e)
		{
			BeginEdit(FormCommand.Edit);
		}
		private void Form_Remove(object sender, EventArgs e)
		{
            Form_Cancel(null, null);
            rowIndex = view.GetRowIndex();
            if (rowIndex == -1) return;

            var deleteRow = view.DisplayList.Rows[rowIndex];
            projectFormManager.Delete(deleteRow["ObjectID"]);
            view.DisplayList.Rows.RemoveAt(rowIndex);
            for (int i = rowIndex; i < view.DisplayList.Rows.Count; i++)
            {
                var row = view.DisplayList.Rows[i];
                row["$_rowIndex"] = i;
            }
        }
		private void Form_Copy(object sender, EventArgs e)
		{
			BeginEdit(FormCommand.Insert);
		}
		private void BeginEdit(FormCommand cmd)
        {
            rowIndex = view.GetRowIndex();
            if (rowIndex == -1) return;

            var selectedRow = view.DisplayList.Rows[rowIndex];
            var ht = view.RowData;
            ht.GetValueFormDataRow(selectedRow);
            view.RowData = ht;
            view.Cmd = cmd;
			view.BeginEdit();
		}
		private void Form_Query(object sender, EventArgs e)
		{
			view.RowData = null;
			view.Cmd = FormCommand.Query;
			view.BeginEdit();
		}
		private void Form_Accept(object sender, EventArgs e)
		{
            bool error = false;
            switch (view.Cmd)
            {
                case FormCommand.Display:
                    break;
                case FormCommand.Insert:
                    var insertEntity = new Model.ProjectForm();
                    view.RowData.FillValueToEntity(insertEntity);
                    projectFormManager.Add(insertEntity);

                    var insertRow = view.DisplayList.NewRow();
                    insertRow.UpdateDataRow(insertEntity);
                    insertRow["$_rowIndex"] = view.DisplayList.Rows.Count;
                    view.DisplayList.Rows.Add(insertRow);
                    break;
                case FormCommand.Edit:
                    var editRow = view.DisplayList.Rows[rowIndex];
                    var editEntity = projectFormManager.Get(editRow["ObjectID"]);
                    view.RowData.FillValueToEntity(editEntity);
                    editEntity.UpdateAt = DateTime.Now;
                    projectFormManager.Update(editEntity);

                    editRow.UpdateDataRow(editEntity);
                    break;
                case FormCommand.Query:
                    var ht = view.RowData;
                    ht.Add("KeyWord", ht["ObjectID"]);
                    ht.Remove("ObjectID");
                    Query(ht);

                    break;
                default:
                    break;
            }
            if (error) return;
            view.RowData = null;
            view.Cmd = FormCommand.Display;
            view.EndEdit();
        }
        private void Query(Hashtable ht)
        {
            var table = projectFormManager.Query(ht);
            view.DisplayList = table;
        }
        private void Form_Cancel(object sender, EventArgs e)
		{
			view.RowData = null;
			view.Cmd = FormCommand.Display;
			view.EndEdit();
		}
		private void Form_Print(object sender, EventArgs e)
		{
			throw new NotImplementedException();
		}
		private void Form_Help(object sender, EventArgs e)
		{
			throw new NotImplementedException();
		}
	}
}
